﻿
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JinfuyuanProject.Repository;
using JinfuyuanProject.Entity;
using System.Data.Entity;



namespace JinfuyuanProject.Repository
{
    public class StaffRepository : IStaffRepository
    {
        private readonly JinfuyuanDbContext _db;

        public StaffRepository(JinfuyuanDbContext db)
        {
            _db = db;
        }
        #region 基础方法
        public IQueryable<Staff> GetList()
        {
            try
            {
                return _db.Staff;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool Delete(Staff staff)
        {
            try
            {

                _db.Entry(staff).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                //记录异常日志
                return false;
            }
        }
        public Staff GetModel(int Id)
        {
            try
            {
                Staff staff = _db.Staff.Find(Id);
                return staff;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int Insert(Staff staff)
        {
            try
            {
                _db.Entry(staff).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return staff.Id;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

        public bool Update(Staff staff)
        {
            try
            {

                _db.Entry(staff).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region 业务方法
        public List<Role> GetAreaChildNode()
        {
            List<Role> childrenareas = _db.Role.ToList();
            return childrenareas;
            
        }
        public IQueryable<StaffVM> StaffAPI()
        {
            var list = _db.Staff.Join(_db.Role, s => s.RoleId, r => r.Id, (s, r) => new StaffVM
            {
                Id = s.Id,
                Numbers = s.Numbers,
                Name = s.Name,
                Phone = s.Phone,
                CardId = s.CardId,
                BankCard = s.BankCard,
                RoleId = s.RoleId,
                State = s.State,
                ImgUrl = s.ImgUrl,
                OnboardingTime = s.OnboardingTime,
                Pwd = s.Pwd,
                Sex = s.Sex,
                Age = s.Age,
                CreateTime = s.CreateTime,
                Isdelete = s.Isdelete,
                RoleName = r.RoleName,
            });
            return list;
        }
        // 获取员工基本信息
        public async Task<Staff> GetEmployeeByIdAsync(int employeeId)
        {
            // 假设员工信息存储在 Staff 表中
            return await _db.Staff
                .Where(e => e.Id == employeeId)
                .FirstOrDefaultAsync();
        }
        #endregion
    }
}

